package nature.nano.control.script.parse;

import java.util.ArrayList;
import java.util.List;

import nature.nano.component.EmptyProps;
import nature.nano.component.SimpleProps;
import nature.nano.component.Strings;
import nature.nano.iface.Accepter;
import nature.nano.component.PrimaryProvider;

public class ScriptInputter extends PrimaryProvider {

	private List<String> rows = new ArrayList<String>();
	
	public ScriptInputter() {
		props(Strings.script);
	}
	
	private void add(String... scripts) {
		for (String script : scripts) {
			List<String> rows = StringHelper.split(script, Strings.semicolon);
			for (String row : rows) {
				this.rows.add(row);
			}
		}
	}
	
	@Override
	public void start() {
		String script = prop(Strings.script);
		if (script!= null) {
			add(script);
		}
		
		Accepter accepter = slot(Strings.output);
		accepter.onBegin(Strings.root, EmptyProps.instance);
		int index = 0;
		for (String row : rows) {
			accepter.onBegin(Strings.row, new SimpleProps(Strings.index, Integer.toString(index)));
			accepter.onContent(row);
			accepter.onEnd(Strings.row);
			index++;
		}
		accepter.onEnd(Strings.root);
	}

}
